var routers = {
    POST: {},
    GET: {},
    getRouter(method, path) {
        var obj = this[method];
        if (!obj) return null;

        if (path in obj) {
            return obj[path];
        }

        if( !path){
            return obj["/"];
        }

        if( path[0] != "/"){
            return obj["/" + path];
        }

        if( path[path.length -1] == "/"){
            return obj[path.slice(0,-1)]
        }

        return null;
    }
};

var post = (url, callback) => {
    routers.POST[url] = callback;
}

var get = (url, callback) => {
    routers.GET[url] = callback;
}


var init = () => {
    const fs = require("fs");
    var path = require('path');

    let mvc = path.normalize(__dirname + "./../routes");

    if (fs.existsSync(mvc) == false) {
        return;
    }


    let files = fs.readdirSync(mvc);


    files.forEach(file => {
        if (!file.endsWith(".js")) return;

        require('../routes/' + file.slice(0, -3))
    });


    logger.info("自动加载 mvc/ ：" + files.map(it => {
        return it.slice(0, -3);
    }).join(","));
}

exports.post = post;
exports.get = get;
exports.routers = routers;
exports.init = init